﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using  Extreme.Mathematics.LinearAlgebra;
using Extreme.Mathematics;

namespace erse253_homework
{
    public partial class ErSE253H1 : BaseWindow
    {

        #region Constructor

        /// <summary>
        /// Default constructor
        /// It initializes the variables.
        /// </summary>
        public ErSE253H1()
        {
            //SplashScreen();
            
            InitializeComponent();
            
            
        }

        private void SplashScreen()
        {
            Thread openingWindow = new Thread(new ThreadStart(OpeningWindow));
            openingWindow.Start();
            Thread.Sleep(5000);
            openingWindow.Abort();
        }

        public void OpeningWindow()
        {
            Application.Run(new OpeningWindow());
        }

        #endregion

        #region Solutions

        /// <summary>
        /// Import the data to the UI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //creates an object to the class responsible to import the excel file
            ImportExcel import = new ImportExcel();

            //open a dialog to let the user select the desired file
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Select file";
            fdlg.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath);
            fdlg.Filter = "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
            fdlg.FilterIndex = 1;

            //if the user selects a file this will be imported to the program.
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                for (int i = 0; i < tcData.TabPages.Count; i++)
                {
                    //imports the file to a datatable and adds it to the UI.
                    DataTable dtdata = new DataTable();
                    DataGridView dgvData = new DataGridView();
                    tcData.TabPages[i].Controls.Clear();
                    tcData.TabPages[i].Controls.Add(dgvData);
                    dtdata = import.Import(fdlg.FileName, tcData.TabPages[i].Text);
                    dgvData.DataSource = dtdata;
                    dgvData.Dock = System.Windows.Forms.DockStyle.Fill;
                }
            }

            
        }

        /// <summary>
        /// Calculate the mean
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void meanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Mean: " + Math.Round(solution.Mean(GetTable()), 4).ToString());
        }

        /// <summary>
        /// Calculate the median
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void medianToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Median: " + Math.Round(solution.Median(GetTable()), 4).ToString());
        }

        /// <summary>
        /// Calculate the mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void modeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Mode: " + Math.Round(solution.Mode(GetTable()), 4).ToString());
        }

        /// <summary>
        /// Calculate the minimum
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void minimumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Minimum: " + Math.Round(solution.Min(GetTable()), 4).ToString());
        }

        /// <summary>
        /// Calculate the maximum
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void maximumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Maximum: " + Math.Round(solution.Max(GetTable()), 4).ToString());
        }

        private void normalHistrogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Histogram histogram = new Histogram(GetTable());
            histogram.Show();
        }

        private void cumulativeHistogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CumulativeHistogram histogram = new CumulativeHistogram(GetTable());
            histogram.Show();
        }

        private void standardDeviatonAboutTheMeanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Std Deviation: " + Math.Round(solution.StdDeviationMean(GetTable()), 4).ToString());
        }

        private void standardDeviationAboutTheMedianToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Std Deviation: " + Math.Round(solution.StdDeviationMedian(GetTable()), 4).ToString());
        }

        private void interquartileRangeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Interquartile Range: " + Math.Round(solution.InterquartileRange(GetTable()), 4).ToString());
        }

        private void coefficienteOfSkewnessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Coefficient of skewness: " + Math.Round(solution.CoefficienfSkeness(GetTable()), 4).ToString());
        }

        private void coefficientToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            MessageBox.Show("Coefficient of variation: " + Math.Round(solution.CoefficienfVariation(GetTable()), 4).ToString());
        }

        private void qqSimilarityPlotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<double[,]> values = GetTables();
            if (values != null)
            {
                qqSimilarity similarity = new qqSimilarity(values[0], values[1]);
                similarity.Show();
            }
        }

        private void scatterplotBetweenBothVariablesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<double[,]> values = GetTables();
            if (values != null)
            {
                Scatterplot scatterplot = new Scatterplot(values[0], values[1]);
                scatterplot.Show();
            }
        }

        private void linearRegressionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NumericalSolutions solution = new NumericalSolutions();
            List<double[,]> values = GetTables();
            if (values != null)
            {
                double a, b;
                solution.LinearRegression(values[0], values[1], out a, out b);
                Scatterplot scatterplot = new Scatterplot(values[0], values[1], a, b);
                scatterplot.Show();
            }
        }

        private void aboutMeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutMe me = new AboutMe();
            me.Show();
        }

        private void generateMapToolStripMenuItem_Click(object sender, EventArgs e)
        {

            double[,] matrix = SparceMatrix();

            for (int i = 0; i < matrix.GetLength(0); i++)
                for (int j = 0; j < matrix.GetLength(1); j++)
                    if (!double.IsNaN(matrix[i, j]))
                        matrix[i, j] = 1;

            Chart sparceMatrix = new Chart();
            ColorMap map = new ColorMap(matrix);
            map.Show();
        }

        private void correlactionCoefficientToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<double[,]> values = GetTables();
            if (values != null)
            {
                NumericalSolutions solution = new NumericalSolutions();
                MessageBox.Show("Correlation Coefficient: " + solution.CorrelationCoefficient(values[0], values[1]).ToString());
            }
        }

        private void covarianceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<double[,]> values = GetTables();
            if (values != null)
            {
                NumericalSolutions solution = new NumericalSolutions();
                MessageBox.Show("Covariance : " + solution.Covariance(values[0], values[1]).ToString());
            }
        }

        private void compareNormalHistogramAgainstLogHistogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Compare compare = new Compare(GetTable(), true, true);
            compare.Show();
        }

        private void compareHistogramsOfBothVariablesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<double[,]> values = GetTables();
            if (values != null)
            {
                Compare compare = new Compare(values[0], values[1], false, false);
                compare.Show();
            }
        }

        private void analysisOfMeanAndStandardDeviationAcrossTheSpacialDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Chart localSTD = new Chart();
            double[,] stdDevian = CalcualteSTDDevian(localSTD, 16);
            ColorMap map = new ColorMap(localSTD.InflateImage(stdDevian, 1024));
            map.Show();
        }

        private void hscatterplotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Chart sparceMatrix = new Chart();
            Variogram variogram = new Variogram(SparceMatrix(), false, 10);
            variogram.Show();
        }

        private void variogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Variogram variogram = new Variogram(GetTable(), true);
            //variogram.Show();
            MessageBox.Show("imp pending!");
        }

        private void covariogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Covariogram covariogram = new Covariogram(GetTable(), true);
            //covariogram.Show();
            MessageBox.Show("fix");
        }

        private void correleogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Correleogram correleogram = new Correleogram(GetTable(), true);
            //correleogram.Show();
            MessageBox.Show("fix");
        }

        private void correleogramToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //Correleogram correleogram = new Correleogram(GetTable(), false);
            //correleogram.Show();
            MessageBox.Show("fix");
        }

        private void covariogramToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //Covariogram covariogram = new Covariogram(GetTable(), false);
            //covariogram.Show();
            MessageBox.Show("fix");
        }

        private void variogramToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Chart sparceMatrix = new Chart();
            Variogram variogram = new Variogram(sparceMatrix.CreateSparceMatrix(ConvertTableToVector(GetTable()), ConvertTableToVector(GetTableIndex(2)), ConvertTableToVector(GetTableIndex(3))), true, 10);
            variogram.Show();
        }

        private void generateContourMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Chart sparceMatrix = new Chart();
            ColorMap map = new ColorMap(sparceMatrix.CreateSparceMatrix(ConvertTableToVector(GetTable()), ConvertTableToVector(GetTableIndex(2)), ConvertTableToVector(GetTableIndex(3))));
            map.Show();
        }

        private void checkForAProportionalEffectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double[] meanV;
            double[] stdV;
            CheckProportionalEffect(out meanV, out stdV);

            ProportionalEffect proportionalEffect = new ProportionalEffect(meanV, stdV);
            proportionalEffect.Show();
        }

        private void comapreRWithSVariogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //CompareVariograms variograms = new CompareVariograms(SparceMatrix(0), SparceMatrix(1), true, 5);
            //variograms.Show();
        }

        private void ErSE253H1_Load(object sender, EventArgs e)
        {

        }

        private void cellDeclusteringToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Chart localSTD = new Chart();
            int size = 20;
            double[,] mean = CalculateMean(localSTD, size);
            NumericalSolutions globalMean = new NumericalSolutions();

            MessageBox.Show("Mean: " + globalMean.Mean(mean).ToString());

        }

        private void chatCellDeclusteringToolStripMenuItem_Click(object sender, EventArgs e)
        {

            //Chart localSTD = new Chart();

            //double[] x = new double[Enum.GetNames(typeof(Sizes)).Length];
            //double[] y = new double[Enum.GetNames(typeof(Sizes)).Length];
            //NumericalSolutions globalMean = new NumericalSolutions();
            //int i = -1;

            //foreach (string size in Enum.GetNames(typeof(Sizes)))
            //{
            //    i++;
            //    double[,] mean = CalculateMean(localSTD, (int)Enum.Parse(typeof(Sizes), size));
            //    y[i] = globalMean.MeanSpecial(mean);
            //    x[i] = (double)((int)Enum.Parse(typeof(Sizes), size))/10;

            //}

            //Auxiliar_Forms.MeanCellDeclustering meanChart = new Auxiliar_Forms.MeanCellDeclustering(x, y);
            //meanChart.Show();
        }


        private void triangleToolStripMenuItem_Click(object sender, EventArgs e)
        {


        }

        private void localMeanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //int size = 50;
            //int i = (int)((6 - .5) * 10);
            //int j = (int)((9.5 - 0.3) * 10);
            // List<PointxyV> points = GetPointList(size, i, j);
            //double mean = 0;
            //int n = 0;
            //foreach (PointxyV p in points)
            //    if (!double.IsNaN(p.V))
            //    {
            //        mean += p.V;
            //        n++;
            //    }

            //if (n > 0)
            //    MessageBox.Show("Mean " + (mean / n).ToString());

        }


        private void chartInverseDistanceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int size = 50;
            int sizeVector = 100;
            int i = (int)((6 - .5) * 10);
            int j = (int)((9.5 - 0.3) * 10);
            List<PointxyV> points = GetPointList(size, i, j, GetTable());

            double[] x = new double[sizeVector];
            double[] y = new double[sizeVector];

            for (int k = 0; k < sizeVector; k++)
            {
                double mean = 0;
                int n = 0;
                double d = 0;
                foreach (PointxyV p in points)
                    if (!double.IsNaN(p.V) && p.distance > 0)
                    {
                        mean += p.V / Math.Pow(p.distance, k);
                        d += 1 / Math.Pow(p.distance, k);
                        n++;
                    }
                y[k] = mean / d;
                x[k] = k;
            }

            Auxiliar_Forms.InverseDistanceMean meanChart = new Auxiliar_Forms.InverseDistanceMean(x, y);
            meanChart.Show();

        }

        private void graphicalAnswerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Chart localSTD = new Chart();
            double[,] mean = CalculateMean(localSTD, 16);
            ColorMap map = new ColorMap(localSTD.InflateImage(mean, 1024));
            map.Show();
        }

        private void hScatterplotToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            hScatterplot h = new hScatterplot(SparceMatrix());
            h.Show();
        }

        private void compareVariogramsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CompareVariograms variograms = new CompareVariograms(SparceMatrix(), true, 5);
            variograms.Show();
        }

  
        #endregion

        #region Methods

        /// <summary>
        /// Update the dataset in use
        /// </summary>
        private double[,] GetTable() 
        {
            TabPage tb = this.tcData.SelectedTab;

            if (tb.Controls != null && tb.Controls.Count > 0)
            {
                DataGridView data = tb.Controls[0] as DataGridView;
                DataTable dtData = data.DataSource as DataTable;
                double[,] values = new double[dtData.Columns.Count, dtData.Rows.Count];

                for (int i = 0; i < dtData.Columns.Count; i++)
                    for (int j = 0; j < dtData.Rows.Count; j++)
                        values[i, j] = Convert.ToDouble(dtData.Rows[j][i].ToString());
                return values;
            }
            return null;
        }

        private List<double[,]> GetTables()
        {
            List<double[,]> values = new List<double[,]>();

            foreach (TabPage tb in tcData.TabPages)
            {
                if (tb.Controls.Count > 0)
                {
                    DataGridView data = tb.Controls[0] as DataGridView;
                    DataTable dtData = data.DataSource as DataTable;

                    double[,] value = new double[dtData.Columns.Count, dtData.Rows.Count];

                    for (int i = 0; i < dtData.Columns.Count; i++)
                        for (int j = 0; j < dtData.Rows.Count; j++)
                            value[i, j] = Convert.ToDouble(dtData.Rows[j][i].ToString());

                    values.Add(value);
                }
                else 
                    return null;
            }
            return values;
        }

        private double[,] GetTableIndex(int k)
        {
            TabPage tb = tcData.TabPages[k];

            if (tb.Controls.Count > 0)
            {
                DataGridView data = tb.Controls[0] as DataGridView;
                DataTable dtData = data.DataSource as DataTable;

                double[,] value = new double[dtData.Columns.Count, dtData.Rows.Count];

                for (int i = 0; i < dtData.Columns.Count; i++)
                    for (int j = 0; j < dtData.Rows.Count; j++)
                        value[i, j] = Convert.ToDouble(dtData.Rows[j][i].ToString());

                return value;

            }
            else
                return null;

            
        }

        private double[] ConvertTableToVector(double[,] table) 
        { 
            double[] vector = new double[table.GetLength(0)*table.GetLength(1)];
            for (int i = 0; i < table.GetLength(0); i++)
                for (int j = 0; j < table.GetLength(1); j++)
                    vector[i + j * table.GetLength(0)] = table[i, j];

            return vector;
        }

        private double[,] SparceMatrix()
        {
            Chart sparceMatrix = new Chart();
            return sparceMatrix.CreateSparceMatrix(ConvertTableToVector(GetTable()), ConvertTableToVector(GetTableIndex(2)), ConvertTableToVector(GetTableIndex(3)));
        }

        private double[,] CalcualteSTDDevian(Chart localSTD, int size)
        {
            int divisionsX;
            int divisionsY;
            double[,] localData = localSTD.CreateSparceMatrix(ConvertTableToVector(GetTable()), ConvertTableToVector(GetTableIndex(2)), ConvertTableToVector(GetTableIndex(3)));
            double[,] mean;
            double[,] stdDevian;

            localSTD.SetParameters(size, out divisionsX, out divisionsY, out mean, out stdDevian, localData);
            localSTD.CalculateMeanStdDeviation(size, divisionsX, divisionsY, mean, stdDevian, localData);
            return stdDevian;
        }

        private double[,] CalculateMean(Chart localSTD, int size)
        {
            int divisionsX;
            int divisionsY;
            //Create a sparce matrix taking in consideration 
            double[,] localData = SparceMatrix();
            double[,] mean;
            double[,] stdDevian;

            localSTD.SetParameters(size, out divisionsX, out divisionsY, out mean, out stdDevian, localData);
            localSTD.CalculateMeanStdDeviation(size, divisionsX, divisionsY, mean, stdDevian, localData);
            return mean;
        }

        private double[,] SparceMatrix(int i)
        {
            Chart sparceMatrix = new Chart();
            return sparceMatrix.CreateSparceMatrix(ConvertTableToVector(GetTableIndex(i)), ConvertTableToVector(GetTableIndex(2)), ConvertTableToVector(GetTableIndex(3)));
        }

        private void CheckProportionalEffect(out double[] meanV, out double[] stdV)
        {
            Chart localSTD = new Chart();

            double[,] mean;
            double[,] stdDevian;

            mean = CalculateMean(localSTD, 16);
            stdDevian = CalcualteSTDDevian(localSTD, 16);

            int numberOfPoints = GetNumberofPoints(mean);

            meanV = new double[numberOfPoints];
            stdV = new double[numberOfPoints];

            SetPoints(meanV, stdV, mean, stdDevian);
        }

        private static void SetPoints(double[] meanV, double[] stdV, double[,] mean, double[,] stdDevian)
        {
            int k = 0;
            for (int i = 0; i < mean.GetLength(0); i++)
                for (int j = 0; j < mean.GetLength(1); j++)
                    if (!double.IsNaN(mean[i, j]))
                    {
                        meanV[k] = mean[i, j];
                        stdV[k] = stdDevian[i, j];
                        k++;
                    }
        }

        private static int GetNumberofPoints(double[,] mean)
        {
            int numberOfPoints = 0;
            for (int i = 0; i < mean.GetLength(0); i++)
                for (int j = 0; j < mean.GetLength(1); j++)
                    if (!double.IsNaN(mean[i, j]))
                        numberOfPoints++;
            return numberOfPoints;
        }

        private double[] CreateStationLocationMap()
        {
            double[] positions = new double[ConvertTableToVector(GetTable()).Length];
            for (int i = 0; i < positions.Length; i++)
                positions[i] = 1;
            return positions;
        }

        public double[,] GetMatrixInUse(double[,] m)
        {
            double[,] matrix = new double[m.GetLength(0), m.GetLength(1)];
            for (int i = 0; i < matrix.GetLength(0); i++)
                for (int j = 0; j < matrix.GetLength(1); j++)
                    matrix[i, j] = m[i, j];

            return matrix;

        }

        public List<PointxyV> CreatePointList(PointxyV p, PointxyV p2, PointxyV p3)
        {
            List<PointxyV> points2 = new List<PointxyV>();
            points2.Clear();
            points2.Add(p);
            points2.Add(p2);
            points2.Add(p3);

            return points2;
        }

        public List<PointxyV> GetPointList(int tolerance, int i, int j, double[,] matrix)
        {

            List<PointxyV> points = new List<PointxyV>();

            for (int k = -tolerance / 2; k < tolerance / 2; k++)
                for (int l = -tolerance / 2; l < tolerance / 2; l++)
                    if ((i + k >= 0) && (j + l >= 0) && (i + k < matrix.GetLength(0)) && (j + l < matrix.GetLength(1)) && !double.IsNaN(matrix[i + k, j + l]))
                        points.Add(new PointxyV(i + k, j + l, matrix[i + k, j + l], i, j));


            return points;
        }

        public List<PointxyV> GetPointList( double[,] matrix)
        {

            List<PointxyV> points = new List<PointxyV>();

            for (int k = 0; k < matrix.GetLength(0); k++)
                for (int l = 0; l < matrix.GetLength(1); l++)
                    if (!double.IsNaN(matrix[k, l]))
                        points.Add(new PointxyV(k, l, matrix[k, l], 0, 0));


            return points;
        }

        #endregion


        private void crossValidateAllToolStripMenuItem_Click(object sender, EventArgs e)
        {

            double[,] matrix = SparceMatrix();
            double[,] estimatedValues = SparceMatrix();
            PointEstimation polygon = new PointEstimation();
            polygon.CrossValidatePolygon(matrix, estimatedValues);
            CrossValidation crossValidation = new CrossValidation(estimatedValues);
            crossValidation.Show();
        }

        private void estimateGridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PointEstimation ordinaryKriging = new PointEstimation();
            double[,] estimatedMatrix = ordinaryKriging.OrdinaryKrigingMethod(SparceMatrix(0));

            ColorMap map = new ColorMap(estimatedMatrix);
            map.Show();
        }

        private void estimateGridToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            PointEstimation simpleKriging = new PointEstimation();
            double[,] estimatedMatrix = simpleKriging.SimpleKrigingMethod(SparceMatrix(0));

            ColorMap map = new ColorMap(estimatedMatrix);
            map.Show();
        }

        private void showSelectedTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CrossValidation cv = new CrossValidation(SparceMatrix(), true);
            cv.Show();
        }

        private void crossvalidateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double[,] matrix = SparceMatrix();
            double[,] estimatedValues = GetMatrixInUse(matrix);

            PointEstimation kriging = new PointEstimation();
            kriging.CrossValidateKriging(matrix, estimatedValues);

            ColorMap crossValidation = new ColorMap(estimatedValues, matrix);
            crossValidation.Show();
        }

        private void estimateGridToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            PointEstimation polygonMethod = new PointEstimation();
            double[,] matrix = polygonMethod.PolygonMethod(SparceMatrix(0), SparceMatrix(0));

            Chart sparceMatrix = new Chart();
            ColorMap map = new ColorMap(matrix);
            map.Show();
        }

        private void crossValidationToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            double[,] matrix = SparceMatrix();
            double[,] estimatedValues = SparceMatrix();
            PointEstimation polygon = new PointEstimation();
            polygon.CrossValidatePolygon(matrix, estimatedValues);
            ColorMap crossValidation = new ColorMap(estimatedValues, SparceMatrix());
            crossValidation.Show();
        }

        private void estimateGridToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            PointEstimation triangulation = new PointEstimation();
            double[,] estimatedValues = triangulation.TrianglulationMethod(SparceMatrix(0), SparceMatrix(0));


            ColorMap map = new ColorMap(estimatedValues);
            map.Show();
        }

        private void crossValidationToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            double[,] matrix = SparceMatrix();
            double[,] estimatedValues = SparceMatrix();
            PointEstimation triangulation = new PointEstimation();
            triangulation.CrossValidateTriangulation(matrix, estimatedValues);
            ColorMap crossValidation = new ColorMap(estimatedValues, matrix);
            crossValidation.Show();
        }

        private void estimateGridToolStripMenuItem4_Click(object sender, EventArgs e)
        {
            PointEstimation inverseSquareDistance = new PointEstimation();
            double[,] estimatedData = inverseSquareDistance.InverseSquaredDistance(SparceMatrix(0), SparceMatrix(0));


            ColorMap map = new ColorMap(estimatedData);
            map.Show();

        }

        private void crossValidationToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            double[,] matrix = SparceMatrix();
            double[,] estimatedValues = SparceMatrix();

            PointEstimation inverseSquaredDistance = new PointEstimation();
            inverseSquaredDistance.CrossValidateInverseSquaredDistance(matrix, estimatedValues);

            ColorMap crossValidation = new ColorMap(estimatedValues, matrix);
            crossValidation.Show();
        }

        private void compareWithOKToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PointEstimation ordinaryKriging = new PointEstimation();
            double[,] estimatedMatrixOrdinaryKriging = ordinaryKriging.OrdinaryKrigingMethod(SparceMatrix(0));
            double[,] estimatedMatrixSimpleKriging = ordinaryKriging.SimpleKrigingMethod(SparceMatrix(0));

            ColorMap crossValidation = new ColorMap(estimatedMatrixOrdinaryKriging, estimatedMatrixSimpleKriging);
            crossValidation.Show();

        }

        private void estimationErrorVarianceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PointEstimation ordinaryKriging = new PointEstimation();
            double[,] estimatedMatrix = ordinaryKriging.OrdinaryKrigingGridEstimateErrorVariance(SparceMatrix(0));

            double[,] matrix = SparceMatrix();

            PointEstimation polygon = new PointEstimation();
            polygon.CrossValidatePolygon(matrix, estimatedMatrix);


            ColorMap map = new ColorMap(estimatedMatrix);
            map.Show();
        }

        private void compare2DistributionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PointEstimation polygonMethod = new PointEstimation();
            double[,] matrix = polygonMethod.PolygonMethod(SparceMatrix(0), SparceMatrix(0));

            double[,] matrixR = polygonMethod.PolygonMethod(SparceMatrix(1), SparceMatrix(1));
            Chart sparceMatrix = new Chart();
            ColorMap map = new ColorMap(matrixR, matrix);
            map.Show();
        }

        private void compare2DistributionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PointEstimation triangulation = new PointEstimation();
            double[,] matrix = triangulation.TrianglulationMethod(SparceMatrix(0), SparceMatrix(0));

            double[,] matrixR = triangulation.TrianglulationMethod(SparceMatrix(1), SparceMatrix(1));
            Chart sparceMatrix = new Chart();
            ColorMap map = new ColorMap(matrixR, matrix);
            map.Show();
        }

        private void compare2DistributionsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            PointEstimation inverseSquaredDistance = new PointEstimation();

            double[,] matrix = inverseSquaredDistance.InverseSquaredDistance(SparceMatrix(0), SparceMatrix(0));

            double[,] matrixR = inverseSquaredDistance.InverseSquaredDistance(SparceMatrix(1), SparceMatrix(1));
            Chart sparceMatrix = new Chart();
            ColorMap map = new ColorMap(matrixR, matrix);
            map.Show();
        
        }

        private void comapre2DistributionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PointEstimation ordinaryKriging = new PointEstimation();
            double[,] matrix = ordinaryKriging.OrdinaryKrigingMethod(SparceMatrix(0));

            double[,] matrixR = ordinaryKriging.OrdinaryKrigingMethod(SparceMatrix(1));
            Chart sparceMatrix = new Chart();
            ColorMap map = new ColorMap(matrixR, matrix);
            map.Show();
        
        }

        private void compare2DistributionsToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            PointEstimation simpleKriging = new PointEstimation();
            double[,] matrix = simpleKriging.SimpleKrigingMethod(SparceMatrix(0));

            double[,] matrixR = simpleKriging.SimpleKrigingMethod(SparceMatrix(1));
            Chart sparceMatrix = new Chart();
            ColorMap map = new ColorMap(matrix, matrixR);
            map.Show();
        
        }


        private void estimationErrorVarianceToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            PointEstimation simpleKriging = new PointEstimation();
            double[,] estimatedMatrix = simpleKriging.SimpleKrigingGridEstimateErrorVariance(SparceMatrix(0));
            double[,] matrix = SparceMatrix();

            PointEstimation polygon = new PointEstimation();
            polygon.CrossValidatePolygon(matrix, estimatedMatrix);

            ColorMap map = new ColorMap(estimatedMatrix);
            map.Show();
        }

        private void blockKrigingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int size = 80;

            double[,] estimatedMatrix = SparceMatrix(0);
            double[,] dataMatrix = SparceMatrix(0);
            PointEstimation kriging = new PointEstimation();
            kriging.OrdinaryKrigingUsingSearchCriteria(size, estimatedMatrix, dataMatrix);

            ColorMap map = new ColorMap(estimatedMatrix);
            map.Show();
        }

        private void blockKrigingToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            int size = 80;

            double[,] estimatedMatrix = SparceMatrix(0);
            double[,] dataMatrix = SparceMatrix(0);
            PointEstimation kriging = new PointEstimation();
            kriging.SimpleKrigingUsingSearchCriteria(size, estimatedMatrix, dataMatrix);

            ColorMap map = new ColorMap(estimatedMatrix);
            map.Show();
        }

        private void blocks10x10ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double[,] estimatedMatrix = SparceMatrix(0);
            double[,] dataMatrix = SparceMatrix(0);
            PointEstimation kriging = new PointEstimation();
            int numberDivisions = 10;
            kriging.EstimateBlockSimpleKriging(estimatedMatrix, dataMatrix, kriging, numberDivisions);

            ColorMap map = new ColorMap(estimatedMatrix);
            map.Show();
        }


        private void block10X10ToolStripMenuItem_Click(object sender, EventArgs e)
        {


            double[,] estimatedMatrix = SparceMatrix(0);
            double[,] dataMatrix = SparceMatrix(0);
            PointEstimation kriging = new PointEstimation();
            int numberDivisions = 10;
            kriging.EstimateBlockOrdinaryKriging(estimatedMatrix, dataMatrix, kriging, numberDivisions);

            ColorMap map = new ColorMap(estimatedMatrix);
            map.Show();


        }

        private void x1ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            double[,] estimatedMatrix = SparceMatrix(0);
            double[,] dataMatrix = SparceMatrix(0);
            int numberDivisions = 10;
            PointEstimation blockKriging = new PointEstimation();
            double blockKrigingMean = blockKriging.EstimateMeanUsingBlockOrdinaryKriging(estimatedMatrix, dataMatrix, numberDivisions);
            MessageBox.Show(blockKrigingMean.ToString());

        }

        private void x2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double[,] estimatedMatrix = SparceMatrix(0);
            double[,] dataMatrix = SparceMatrix(0);
            int numberDivisions = 20;
            PointEstimation blockKriging = new PointEstimation();
            double blockKrigingMean = blockKriging.EstimateMeanUsingBlockOrdinaryKriging(estimatedMatrix, dataMatrix, numberDivisions);
            MessageBox.Show(blockKrigingMean.ToString());
        }

        private void x4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double[,] estimatedMatrix = SparceMatrix(0);
            double[,] dataMatrix = SparceMatrix(0);
            int numberDivisions = 40;
            PointEstimation blockKriging = new PointEstimation();
            double blockKrigingMean = blockKriging.EstimateMeanUsingBlockOrdinaryKriging(estimatedMatrix, dataMatrix, numberDivisions);
            MessageBox.Show(blockKrigingMean.ToString());
        }

        private void krigingTheVarianceToolStripMenuItem_Click(object sender, EventArgs e)
        {

            PointEstimation KrigingVariance = new PointEstimation();
            List<PointxyV> vectorData;
            double[,] dataMatrix = SparceMatrix(0);
            KrigingVariance.GetDataPoints(dataMatrix, out vectorData);

            double estimatedVar = KrigingVariance.EstimateVarianceKriging( vectorData);

            MessageBox.Show(estimatedVar.ToString());
        }

        private void x1ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            PointEstimation krigingMean = new PointEstimation();
            List<PointxyV> vectorData;
            double[,] dataMatrix = SparceMatrix(0);
            krigingMean.GetDataPoints(dataMatrix, out vectorData);

            double estimatedValue = krigingMean.EstimateMeanKriging(vectorData);

            MessageBox.Show(estimatedValue.ToString());
        }


    }
}
